package com.hsp.Demo_02_array;

import java.util.Scanner;

public class Demo_07 {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3};
        System.out.println("原数组的值：");
        for(int i = 0; i < arr1.length; i++){
            System.out.print(arr1[i] + " ");
        }
        do {
            System.out.println();
            Scanner scanner = new Scanner(System.in);
            System.out.print("请问是否需要添加一个元素[y/n]:");
            String str = scanner.next();
//        if("y".equals(str)){
            if (!"y".equals(str)) {
                System.out.println("不再继续添加，流程结束！");
                break;
            }
            System.out.print("请输入元素的值，必须是整数：");
            int num = scanner.nextInt();
            int[] arrNew = new int[arr1.length + 1];
            for (int i = 0; i < arr1.length; i++) {
                arrNew[i] = arr1[i];
            }
            arrNew[arr1.length] = num;
            arr1 = arrNew;
            System.out.println("添加结束，添加后的数组如下：");
            for (int i = 0; i < arr1.length; i++) {
                System.out.print(arr1[i] + " ");
            }
//        }
        }while(true);

    }
}
